package gui.reader;

import dao.SQLiteConnection;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;

public class MyBorrowWin extends JFrame {
    JPanel upper,middle,lower;
    JLabel user;
    JButton select;
    JTable table;
    DefaultTableModel model;
    String username;

    public MyBorrowWin(String a){
        username=a;
        init();
        setTitle("查看借阅信息");
        setSize(800, 500);
        setLocationRelativeTo(null);    //窗口居中
        setVisible(true);
        setResizable(false);    //禁用用户调整界面大小
    }

    private void init() {

        //搜索框实现在顶部
        upper=new JPanel(new FlowLayout());

        select=new JButton("刷新");
        upper.add(select);

        //图书信息表展示在中部
        middle=new JPanel();
        //图书信息表
        table = new JTable();
        model = new DefaultTableModel();  //表格模型

        table.getTableHeader().setReorderingAllowed(false);  //表格列禁止拖动
        table.setPreferredScrollableViewportSize(new Dimension(600, 400)); // 设置表格宽度和高度

        // 创建表格列名 添加表头
        String[] columnNames = {"借阅编号","书名","ISBN","借出时间","期限时间","归还时间"};
        model.setColumnIdentifiers(columnNames);

        // 将表格组件放在滚动面板中
        JScrollPane scrollPane = new JScrollPane(table);
        middle.add(scrollPane);
        intable();


        lower=new JPanel();
        user=new JLabel("欢迎"+username+"使用本系统");
        lower.add(user);

        add(upper,BorderLayout.NORTH);
        add(middle);
        add(lower,BorderLayout.SOUTH);

    }

    public void intable() {

        String sql1 = "select Borrow.bid,Book.bookname,Borrow.ISBN,Borrow.btime,Borrow.ltime,Borrow.rtime " +
                "from Borrow JOIN Book ON Book.ISBN=Borrow.ISBN " +
                "where rid=" + username;
        String sql="SELECT Borrow.bid,Book.bookname, Record.ISBN,Borrow.btime,Borrow.ltime,Borrow.rtime " +
                "from Borrow " +
                "inner join Record on Record.bid=Borrow.bid " +
                "inner join Reader on Reader.rid=Record.rid " +
                "inner join Book on Book.ISBN=Record.ISBN " +
                "where Record.rid='" + username+"'";
        //连接数据库
        SQLiteConnection connection = new SQLiteConnection();
        connection.Connect_SQLite();
        // 读取数据库中的数据
        try {
            Connection conn = connection.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            //清除原表格中的内容
            model.setRowCount(0);
            //添加新数据
            ResultSetMetaData meta = rs.getMetaData();
            int columnCount = meta.getColumnCount();
            // 添加数据
            while (rs.next()) {
                Object[] rowData = new Object[columnCount];
                for (int i = 1; i <= columnCount; i++) {
                    rowData[i - 1] = rs.getObject(i);
                }
                model.addRow(rowData);
            }
            table.setModel(model);    // 设置表格的数据和显示
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        connection.closeConnection();
    }


    public JButton getSelect() {
        return select;
    }

    public DefaultTableModel getModel() {
        return model;
    }

}
